package com.miao.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.miao.admin.entity.vo.ClassificationVO;
import com.miao.admin.service.AdminClassificationService;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Classification;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 商品类型(Classification)表控制层
 *
 * @author MiaoWenqing
 * @since 2021-02-03 09:43:10
 */
@RestController
@RequestMapping("admin/classification")
public class AdminClassificationController {
    /**
     * 服务对象
     */
    @Resource
    private AdminClassificationService adminClassificationService;

    /**
     * 获取全部分类
     *
     * @return
     */
    @GetMapping("/all")
    public R classificationAll() {
        List<Classification> classificationList = this.adminClassificationService.list();
        ArrayList<ClassificationVO> classificationVOS = new ArrayList<>();
        classificationList.forEach(item -> {
            //如果 类型级别为1 则为父类
            if (item.getClassGrade() == 1) {
                ClassificationVO classificationVO = new ClassificationVO();
                classificationVO.setClassGrade(1);
                classificationVO.setClassId(item.getClassId());
                classificationVO.setClassName(item.getClassName());
                ArrayList<ClassificationVO> children = new ArrayList<>();
                classificationVO.setChildren(children);
                classificationVOS.add(classificationVO);
            }
            //如果 类型级别为2 则为子类
            if (item.getClassGrade() == 2) {
                //找到 id与父类
                for (int i = 0; i < classificationVOS.size(); i++) {
                    ClassificationVO classificationFather = classificationVOS.get(i);
                    if (classificationFather.getClassId().equals(item.getClassParent())) {
                        ClassificationVO classificationVO = new ClassificationVO();
                        classificationVO.setClassGrade(2);
                        classificationVO.setClassId(item.getClassId());
                        classificationVO.setClassName(item.getClassName());
                        classificationFather.getChildren().add(classificationVO);

                    }
                }
            }
        });


        return R.ok(classificationVOS);
    }

    @GetMapping("/father")
    public R classFather() {
        List<Classification> classifications = this.classificationFather();
        return R.ok(classifications);
    }

    @GetMapping("/children")
    public R classChildren(String classId) {
        List<Classification> classifications = this.classificationChildren(classId);
        return R.ok(classifications);
    }


    @PostMapping("/edit")
    public R classificationEdit(@RequestBody Classification classification) {

        Classification classificationOld = this.adminClassificationService.getById(classification.getClassId());

        classificationOld.setClassName(classification.getClassName());
        if (this.adminClassificationService.saveOrUpdate(classificationOld)) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/add")
    public R classificationAdd(@RequestBody Classification classification) {

        String classParentId = classification.getClassParent();
        if (!classParentId.equals("-1")) {
            int size = this.classificationChildren(classParentId).size();
            int classificationId = Integer.parseInt(classParentId);
            classificationId += size + 1;
            classification.setClassId(String.valueOf(classificationId));
            classification.setClassGrade(2);
            if (this.adminClassificationService.save(classification)) {
                return R.ok();
            } else {
                return R.error();
            }
        } else {
            int size = this.classificationFather().size();
            String classificationId = (size + 1) + "0000";
            classification.setClassId(classificationId);
            classification.setClassGrade(1);
            if (this.adminClassificationService.save(classification)) {
                return R.ok();
            } else {
                return R.error();
            }
        }
    }

    /**
     * 获取一级
     *
     * @return list
     */
    public List<Classification> classificationFather() {

        QueryWrapper<Classification> classificationQueryWrapper = new QueryWrapper<>();
        classificationQueryWrapper.eq("class_grade", 1);
        return this.adminClassificationService.list(classificationQueryWrapper);
    }

    /**
     * 获取二级
     *
     * @param classId 父id
     * @return list
     */
    public List<Classification> classificationChildren(String classId) {

        QueryWrapper<Classification> classificationQueryWrapper = new QueryWrapper<>();
        classificationQueryWrapper.eq("class_grade", 2);
        classificationQueryWrapper.eq("class_parent", classId);
        return this.adminClassificationService.list(classificationQueryWrapper);
    }


}